<!--
Computer Science Course 531 - Introduction to Software Engineering
Olive Insurance Client Management System (Client Database Access)
Version 1.0 (Base System)
Spring 2011

-- Source Code Details --
Page Tile: agentAccountDetails.php
Created By: David Gonzalez, Computer Science (Undergraduate)
Documented By: Darrius Serrant, Computer Science (Undergraduate)
Purpose: User interface allowing the system administrator add, remove, and modify
        user accounts.
Status: In Evaluation: All security blocks, form validation, and database
         connectivity features have been omitted for the time being to illustrate
         the application's basic flow and functionality.
-->
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Agent Database:</title>
        <SCRIPT LANGUAGE="JavaScript" SRC="CalendarPopup.js"></SCRIPT>
	<SCRIPT LANGUAGE="JavaScript">
	var cal = new CalendarPopup();
        </SCRIPT>
    </head>
    <h2>Agent Database:</h2>
    <body>
        <?php
        session_start();
        require_once('connectvars.php');
        $display = "yes";
        $regex = '/^.*(?=.{7,14})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#!$%^&+=]).*$/';
        if(isset($_SESSION['user_name'])){
            if(isset($_POST['save'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $aname = mysqli_real_escape_string($dbc,trim($_POST['aname']));
            $hired = mysqli_real_escape_string($dbc,trim($_POST['hired']));
            $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
            $password = mysqli_real_escape_string($dbc,trim($_POST['password']));
            if(isset($_POST['DISABLED'])) $DISABLED = 1; else $DISABLED = 0;
        if(!empty ($aname) && !empty ($hired) && !empty ($username) && !empty ($password)){
            if(preg_match($regex, $password)){
                $query = "INSERT INTO agent (Name, Date_Hired, Username, Password, DISABLED)" .
                "VALUES ('$aname','$hired','$username',SHA('$password'), '$DISABLED')";
                $data = mysqli_query($dbc, $query) or die("Error Saving");

                mysqli_close($dbc);
                echo "Record Saved <br>";
           }else echo "Password criteria not met!";
        }
        else{
            echo "All fields need to be entered!<br>";
        }
        }
        if(isset($_POST['delete'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $aname = mysqli_real_escape_string($dbc,trim($_POST['aname']));
            $hired = mysqli_real_escape_string($dbc,trim($_POST['hired']));
            $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
            $password = mysqli_real_escape_string($dbc,trim($_POST['password']));

        if(!empty ($username)){
            $query = "SELECT * FROM agent WHERE Username = '$username'";
            $result = mysqli_query($dbc, $query) or die("Error");
            ?><form action ="agentAccountDetails.php" method="post"><?php
            while($row = mysqli_fetch_array($result)){
                echo '<input type ="radio" value="'.$row['id'].
                '" name ="todelete[]" />';
                echo $row['Username'].'<br/>';
            }
            ?>

        <input type="submit" name="remove" value="Remove"/>
        <input type="submit" name="reload" value="Back"/>
        </form>
        <?php
            $display = "no";
        }else{
            mysqli_close($dbc);
            echo "Must have Username";
        }
        }
        if(isset($_POST['remove'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
            foreach($_POST['todelete'] as $deleteid){
                $query = "DELETE FROM agent WHERE id = '$deleteid'";
                mysqli_query($dbc,$query) or die("ERROR DELETING");
            }
            $display = "yes";
            mysqli_close($dbc);
            echo "Record Deleted";
        }
        if(isset($_POST['find'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
            foreach($_POST['toupload'] as $uploadid){
                $query = "SELECT * FROM agent WHERE id = '$uploadid'";
                $data = mysqli_query($dbc,$query) or die("ERROR Finding");
                $row = mysqli_fetch_array($data);
            }
            $display = "No";
            echo "Record Uploaded";
            ?>
        <form action="agentAccountDetails.php" method ="post" name="Agent">
        <label for ="aname">Agent Name:</label>
        <input type ="text" id="aname" name ="aname" value ="<?php echo $row['Name']?>"/>
        <label for ="hired">     Date Hired:</label>
        <input type ="text" id ="hired" name ="hired" value ="<?php echo $row['Date_Hired']?>"
               READONLY/>
        <A HREF="#"
        onClick="cal.select(document.forms['Agent'].hired,'anchor1','yyyy-MM-dd'); return false;"
        NAME="anchor1" ID="anchor1">select</A>
        <br/>
        <label for ="username">Username:</label>
        <input type ="text" id="username" name ="username" value ="<?php echo $row['Username']?>"/>
        <label for ="password">     Password:</label>
        <input type ="text" id ="password" name ="password" value =""/><br/>
        <input type ="hidden" value="<?php echo $row['id']?>" name="id"/>
        <?php if($row['DISABLED'] == 0) echo '<input type="checkbox" name="DISABLED">DISABLED<br/>';
            else echo '<input type="checkbox" name="DISABLED" CHECKED>DISABLED<br/>';?>
        <input type ="submit" value="Update" name ="update" />
        <input type ="submit" value="Save" name ="save" />
        <input type ="submit" value="Delete" name ="delete" />
        <input type ="submit" value="Search" name ="search" />
        <input type ="submit" value="Back" name ="back" />
        </form>
        <?php
            mysqli_close($dbc);
        }
        if(isset($_POST['update'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $aname = mysqli_real_escape_string($dbc,trim($_POST['aname']));
            $hired = mysqli_real_escape_string($dbc,trim($_POST['hired']));
            $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
            $password = mysqli_real_escape_string($dbc,trim($_POST['password']));
            if(isset($_POST['DISABLED'])) $DISABLED = 1; else $DISABLED = 0;
            $id = mysqli_real_escape_string($dbc,trim($_POST['id']));

            if(!empty ($aname) && !empty ($hired) && !empty ($username)){
                if($password == ""){
                $query = "UPDATE agent SET Name = '$aname', Date_Hired = '$hired', Username = '$username',".
                "DISABLED = '$DISABLED' WHERE id = '$id'";
                $data = mysqli_query($dbc, $query) or die("Error Updating1");

                mysqli_close($dbc);
                echo "Record Updated <br>";
                }else{
                    if(preg_match($regex, $password)){
                    $query = "UPDATE agent SET Name = '$aname', Date_Hired = '$hired', Username = '$username',".
                    "Password = SHA('$password') DISABLED = '$DISABLED' WHERE id = '$id'";
                    $data = mysqli_query($dbc, $query) or die("Error Updating2");

                    mysqli_close($dbc);
                    echo "Record Updated <br>";
                    }else echo "Password criteria not met!";
                }
        }
        else{
            echo "All fields need to be entered!<br>";
        }
        }
        if(isset($_POST['search'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $aname = mysqli_real_escape_string($dbc,trim($_POST['aname']));
            $hired = mysqli_real_escape_string($dbc,trim($_POST['hired']));
            $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
            $password = mysqli_real_escape_string($dbc,trim($_POST['password']));

        if(!empty ($username)){
            $query = "SELECT * FROM agent WHERE Username = '$username'";
            $result = mysqli_query($dbc, $query) or die("Error");
            ?><form action ="agentAccountDetails.php" method="post"><?php
            while($row = mysqli_fetch_array($result)){
                echo '<input type ="radio" value="'.$row['id'].
                '" name ="toupload[]" />';
                echo $row['Username'].'<br/>';
            }
            ?>

        <input type="submit" name="find" value="Find"/>
        <input type="submit" name="reload" value="Back"/>
        </form>
        <?php
            $display = "no";
        }else{
            mysqli_close($dbc);
            echo "Must have Username";
        }
        }
        if(isset($_POST['reload'])){
            header('Location: http://localhost:8888/PHP3/agentAccountDetails.php');
        }
        if(isset($_POST['back'])){
            header('Location: http://localhost:8888/PHP3/mainPage.php');
        }
        if($display == "yes"){?>
        <form action="agentAccountDetails.php" method ="post" name="Agent">
        <label for ="aname">Agent Name:</label>
        <input type ="text" id="aname" name ="aname"/>
        <label for ="hired">     Date Hired:</label>
        <input type ="text" id ="hired" name ="hired" READONLY/>
        <A HREF="#"
        onClick="cal.select(document.forms['Agent'].hired,'anchor1','yyyy-MM-dd'); return false;"
        NAME="anchor1" ID="anchor1">select</A>
        <br/>
        <label for ="username">Username:</label>
        <input type ="text" id="username" name ="username"/>
        <label for ="password">     Password:</label>
        <input type ="text" id ="password" name ="password"/><br/>
        <input type="checkbox" name="DISABLED">DISABLED<br/>
        <input type ="submit" value="Save" name ="save" />
        <input type ="submit" value="Delete" name ="delete" />
        <input type ="submit" value="Search" name ="search" />
        <input type ="submit" value="Back" name ="back" />
        </form>
        <?php
        }
        }else{
            echo 'You aren\'t logged in! <br>';
            header('Refresh: 2; http://localhost:8888/PHP3/signIn.php');
            echo 'You will be transfered to Sign In page...';
        }
        ?>
    </body>
</html>